/*
 * Created on 2006-5-17 15:34:17
 */
package cn.com.sbell.vss.csg.vap;

import java.util.ArrayList;

import org.dom4j.Element;

import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.csg.vap.bean.LocalStorageTask;
import cn.com.sbell.vss.csg.vap.bean.RecordSchedule;
import cn.com.sbell.vss.csg.vsclient.VsClient;
import cn.com.sbell.vss.csg.vsclient.util.BeanValidator;
import cn.com.sbell.vss.csg.vsclient.util.PropertiesHolder;

/**
 * @author liang.a.chen
 */
public class SetVsLocalStorageTask extends ProtectedVsCommander {

	protected Element executeCommand(VsClient vsClient, PropertiesHolder holder, Element root) {
		LocalStorageTask localTask = new LocalStorageTask();
		localTask.setVideoId(holder.getIntProperty("videoId"));
		localTask.setDiskFullThreshold(holder.getIntProperty("diskFullThreshold"));
		localTask.setFilenameSuffix(holder.getStrProperty("filenameSuffix"));
		localTask.setMjpegFrame(holder.getIntProperty("mjpegFrame"));
		localTask.setMpeg4Frame(holder.getIntProperty("mpeg4Frame"));
		localTask.setRecordPolicy(holder.getIntProperty("recordPolicy"));
		localTask.setRecycleRecEnable(holder.getIntProperty("recycleRecEnable"));
		localTask.setRecordEnable(new Integer(1));
		
		BeanValidator.validateNotNull(
				localTask, 
				new String[]{
						"videoId",
						"recycleRecEnable"
				}
		);
		ArrayList schedList = new ArrayList();
		String[] schedule = holder.getStrProperties("schedule");
		if(schedule == null) schedule = new String[0];
		for(int i=0;schedule!=null && i<schedule.length;i++){
			String[] s = schedule[i].split(",");
			if(!"0".equals(s[0])) continue;
			RecordSchedule recordSchedule = new RecordSchedule();
			try{
				recordSchedule.setRecordType(Integer.parseInt(s[0]));
				recordSchedule.setWeekday(Integer.parseInt(s[1]));
				recordSchedule.setStartHour(Integer.parseInt(s[2]));
				recordSchedule.setStartMin(Integer.parseInt(s[3]));
				recordSchedule.setStopHour(Integer.parseInt(s[4]));
				recordSchedule.setStopMin(Integer.parseInt(s[5]));
			}catch(Exception e){
				
			}			
			schedList.add(recordSchedule);
		}
		localTask.setSchedule((RecordSchedule[])schedList.toArray(new RecordSchedule[0]));
		if(!vsClient.setVsLocalStorageTask(localTask)){
			throw new VssException(VssConstants.CODE_SET_VS_LOCAL_STORAGE_TASK_FAILED);
		}
		return root;
	}

	
}
